import sys
import loguru
from os_package import Os_Option
from datetime import date
from datetime import datetime
import atexit


class Logging_Option(object):
    def __init__(self):
        os_option = Os_Option()
        # 注册程序绑定，程序一但退出则执行
        atexit.register(self.__del__)

        if not os_option.get_isexist("log"):
            os_option.make_file_folder("log")

        loguru.logger.add(
            f'./log/{os_option.get_file_name_and_extension(sys.argv[0])[0]}_{date.today().year}_{date.today().month}_{date.today().day}.log',
            rotation="10 MB",
            encoding='utf-8',

            # format="{time} | {level} | {message}"
        )
        # loguru.logger.add(f'./src/{datetime.now():%Y-%m-%d_%H-%M-%S}.log', rotation="10 MB")

        del os_option

        self.write_warning('______________________________')
        self.write_warning('==========开始记录日志==========')

    def __del__(self):
        self.write_warning('==========停止记录日志==========')
        self.write_warning('______________________________')

    def write_info(self, message):
        loguru.logger.info(message)

    def write_warning(self, message):
        loguru.logger.warning(message)

    def write_error(self, message):
        loguru.logger.error(message)

    def write_debug(self, message):
        loguru.logger.debug(message)

    def write_success(self, message):
        loguru.logger.success(message)
